package com.sailorj.service.impl;

import com.sailorj.entity.TbStudent;
import com.sailorj.mapper.TbStudentMapper;
import com.sailorj.service.TbStudentService;
import com.sailorj.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbStudentServiceImpl implements TbStudentService {

    @Autowired
    public TbStudentMapper tbStudentMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void addStudent(TbStudent tbStudent) {
        tbStudentMapper.insert(tbStudent);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void updateStudentById(TbStudent tbStudent) {
        tbStudentMapper.updateByPrimaryKey(tbStudent);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public TbStudent getStudentById(int id) {
        return tbStudentMapper.selectByPrimaryKey(id);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public PageBean getStudentList(Integer page, Integer size) {
        if(page!=null&&size!=null){
            page=(page-1)*size;
        }
        List<TbStudent> data = tbStudentMapper.selectStudentList(page,size);
        Long total=tbStudentMapper.getTotal();
        PageBean bean = new PageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }
}
